package routers

import (
	"github.com/gin-gonic/gin"

	"github.com/myblogs/controllers"
	"github.com/myblogs/dao/mysql"
	"github.com/myblogs/logger"
	"github.com/myblogs/logic"
	"github.com/myblogs/middlewares"
)

type Router struct {
	engine *gin.Engine
}

func NewRouter() *Router {
	return &Router{
		engine: gin.Default(),
	}
}

func (r *Router) RegisterRouter() {
	r.engine.Use(logger.GinLogger(), logger.GinRecovery(true)).Use(middlewares.Cors())
	api := r.engine.Group("/api")
	{
		user := api.Group("/user")
		userController := controllers.NewUserController(logic.NewUserLogic(mysql.NewUserDao()))
		{
			// user.POST("/login", controllers.Login)
			user.POST("/register", userController.RegisterHandler)
			user.POST("/login", userController.LoginHandler)
		}

		tag := api.Group("/tag")
		tagControllers := controllers.NewTagController(logic.NewTagLogic(mysql.NewTagDao()))
		{
			tag.POST("/create", tagControllers.CreateTagHandler)
			tag.GET("/all", tagControllers.GetAllTagsHandler)
		}
		article := api.Group("/article").Use(middlewares.JWTAuthMiddleware())
		articleControllers := controllers.NewArticleHandleImpl(logic.NewArticleLogicImpl(mysql.NewArticleDaoImpl()))
		{
			article.POST("/create", articleControllers.CreateArticlaHandler)
			article.GET("/:id", articleControllers.GetArticleByIDHandler)
			article.GET("/all", articleControllers.GetAllArticlesHandler)
			article.GET("/userartcle", articleControllers.GetArticleByUserIDHandler)
		}
	}

}

func (r *Router) GetEngine() *gin.Engine {
	return r.engine
}
